<?php
    /**
     * Author: H J
     * Date: 2016/11/23 15:47
     * Description:
     */

namespace App\Http\Service;
use App\Models\GroupInformation;
use App\Models\GroupInfomationTag;
use Illuminate\Support\Facades\DB;


class GroupInformationService {

    public static function informationListPageinit($pageSize=DEFAULT_PAGESIZE,$where=[],$searchVal=null){
        $infomationEntity=GroupInformation::where($where)
            ->orderby('sort','DESC')
            ->orderby('id','DESC');
        if(!empty($searchVal)){
            return $infomationEntity->where('name','like','%'.$searchVal.'%')
                ->paginate($pageSize);
        }
        return $infomationEntity->paginate($pageSize);
    }

    public static function getSourceList(){
        return GroupInformation::getSourceList();
    }

    public static function getTagList(){
        return GroupInformation::getTagList();
    }

    public static function getVerifyStatus(){
        return GroupInformation::getVerifyStatus();
    }

    public static function createOneInformation($infoData){
        if(!empty($infoData['tag'])){
            $tag=$infoData['tag'];
            $infoData['tag']=implode(';',$infoData['tag']);
         }
        DB::beginTransaction();
        try{
            $mod_Infomation = GroupInformation::create($infoData);
            if(!empty($tag)){
                $arr_Tag = [];
                foreach($tag as $int_TagId){
                    $arr_Tag[] = [
                        'tag_id' => $int_TagId,
                        'information_id' => $mod_Infomation->id
                    ];
                }
                DB::table('group_information_tag')->insert($arr_Tag);
            }
        }catch (\Exception $e){
            DB::rollback();
            return false;
        }
        DB::commit();
        return true;
    }

    public static function getOneInformation($infoId){
        $data=GroupInformation::find($infoId);
        return $data;
    }

    public static function getInformation($where=[]){
        $data = GroupInformation::where($where)
            ->orderby('sort','DESC')
            ->orderby('id','DESC')
            ->get();
        return $data;
    }

    public static function updateOneInformatin($infoData){
        if(!empty($infoData['tag'])){
            $infoData['tag']=implode(';',$infoData['tag']);
        }
        return GroupInformation::find($infoData['id'])
                    ->update($infoData);
    }

    public static function deleteOneInformation($infoId){
        return GroupInformation::find($infoId)
                    ->update(['isdelete'=>1]);
    }

    public static function updateViderUrl($infoDate){
        return GroupInformation::find($infoDate['id'])
            ->update(['video_url'=>$infoDate['video_url']]);
    }

    public static function getInformationByTag($tagId,$pageSize=DEFAULT_PAGESIZE,$page=1){
        return GroupInfomationTag::where(['group_information_tag.tag_id'=>$tagId,'isdelete'=>0,'verify_status'=>1])
                    ->join('group_information', 'group_information_tag.information_id', '=', 'group_information.id')
                    ->orderby('group_information.sort','DESC')
                    ->orderby('group_information.id','DESC')
                    ->forpage($page,$pageSize)
                    ->select([
                        'group_information.id as id',
                        'group_information.name as name',
                        'group_information.summary as summary',
                        'group_information.logo as logo',
                        'group_information.tag as tag',
                        'group_information.source as source',
                        'group_information.source_url as source_url',
                        'group_information.video_url as video_url',
                        'group_information.sort as sort',
                        'group_information.show_time as show_time',

                        'group_information_tag.tag_id as tag_id',
                    ])
                    ->get();
    }

    public static function getInformationForPage($pageSize=DEFAULT_PAGESIZE,$page=1,$where=[],$whereIn=[]){
         $entity=GroupInformation::where($where)
                        ->orderby('sort','DESC')
                        ->orderby('id','DESC')
                        ->forPage($page,$pageSize);
         if(!empty($whereIn)){
             foreach ($whereIn as $key => $val){
                 $entity->whereIn($key,$val);
             }
         }
         return  $entity->get();
    }

    public static function getInformationCount($where=[],$whereIn=[]){
        $entity=GroupInformation::where($where);
        if(!empty($whereIn)){
            foreach ($whereIn as $key => $val){
                $entity->whereIn($key,$val);
            }
        }
        return  $entity->count();
    }


}